package mysql

import (
	"box-fresh/boxrpc/boxrpc"

	"gorm.io/gorm"
)

type Fresh struct {
	gorm.Model
	Name    string  `gorm:"type:varchar(30);not null;comment:药品名称"`
	Price   float32 `gorm:"type:decimal(10,2);not null;comment:价格"`
	Image   string  `gorm:"type:varchar(30);not null;comment:图片"`
	TypesId int64   `gorm:"type:varchar(30);not null;comment:类型id"`
}

func (f *Fresh) FreshAdd(db *gorm.DB) error {
	return db.Debug().Create(f).Error
}

func (f *Fresh) FreshShow(db *gorm.DB, name string) error {
	return db.Debug().Where("name=?", name).First(f).Error
}

// 两表连查
func (f *Fresh) FreshList(db *gorm.DB, in *boxrpc.GoodsListRequest) []*boxrpc.GoodsList {
	var list []*boxrpc.GoodsList
	db.Debug().Model(&f).
		Select(
			"freshes.id as goods_id",
			"freshes.name",
			"price",
			"image",
			"classes.name as class_name").
		Joins("JOIN classes ON freshes.types_id = classes.id").
		Scopes(Paginate(int(in.Page), int(in.Size))).Find(&list)
	return list
}
